<?php
/*------------------------------------------------------------------------------ 
    Create Date :   25/10/2010
    Author    :      Nguyen Hoang Phuc
    Copyright(c) 2010 A.D.A Solution. All rights reserved
     
      
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#             Date          Author       Note
    Lephan  TRB1           2010/01/20      HoangPhuc    Xử lý xem bản đồ trường hợp Phương tiện có check tổng hợp
------------------------------------------------------------------------------*/
if (!defined('IN_ADAGPS')){
    die(SYSTEM_ACCESS_DENIED);
} 

global $objTemplate, $objDbSelect,$arrConfig;   

// dang ky su dung ham xajax   
$objXajax->registerFunction('ajxUpdateData');
$objXajax->registerFunction('ajxLoadData');
// load dữ liệu
function ajxLoadData($load = 0)
{
   global $objDbSelect,$objTemplate;
   try
  {
   $now = date('Y-m-d');
   $SQL = "DELETE FROM tbl_current_employee WHERE createdate != '$now'";
   $rsd = $objDbSelect->Execute($SQL);    
   // load danh sach nhân viên 
   $sql = "SELECT id,username,url_image FROM tbl_employee WHERE id not in (SELECT id_employee FROM tbl_current_employee)";   
   $xjax_respone = new xajaxResponse();
   $rs = $objDbSelect->Execute($sql);
   $sScript = ''; 
   if($rs->RecordCount() > 0)
   {
       do
       {
            $id = $rs->fields['id']; 
            $url_image = $rs->fields['url_image'];
            $username = $rs->fields['username'];
            $sScript .='<div  id="'.$id.'" class="gallery" style="width:96px;height:72px;display: inline-block;padding-right: 10px;margin-top: 0px;">';
            $sScript .= ' <img src="'.$url_image.'" id="'.$id.'" style="width:96px;height:72px"/>';
            $sScript .= '<h5 class="ui-widget-header" style="width:95x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
            $sScript .= '</div>';   
       }                                           
       while($rs->MoveNext());    
   }
    $xjax_respone = new xajaxResponse();     
    $xjax_respone->addAssign('gallery', 'innerHTML', $sScript);
    $js = "";  
   // load nhan vien nghi lam
       // trash 1 
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 0";      
       $rs = $objDbSelect->Execute($sql);
       $sScript = '';  
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : ''; 
                $username = $rs->fields['username'];  
                $sScript .= '';
                if ($url_image != '')
                {
                  $sScript .='<div  id="'.$id_image.'" class="gallery" style="width:48px;height:36px;display: inline-block;padding-right: 10px;margin-top: 0px">';        
                  $sScript .= ' <img src="'.$url_image.'" id="'.$id_image.'" style="width:48px;height:36px"/> ';   
                  $sScript .= '<h5 class="ui-widget-header" style="width:47x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
                  $sScript .= '</div>';      
                }
                $sScript .= '';
           }                                           
           while($rs->MoveNext());    
       }
        $i = 1;  
        $js .=' var $trash'.$i.' = $( "#trash'.$i.'" );    
                      $( "div", $trash'.$i.' ).draggable({
                        revert: "invalid", 
                        containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
                        helper: "clone",
                        cursor: "auto"
                        });
                      $trash'.$i.'.droppable({
                       activeClass: "ui-state-highlight",
                       drop: function( event, ui ) {
                        var a = $.trim($($(this).find("img").get(0)).attr("id"));    
                        var b = $.trim($($(ui.draggable).get(0)).attr("id"));
                        deleteImage( ui.draggable,$trash'.$i.' ); 
                        xajax_ajxUpdateData(b,0);
                      }
                });';
       $xjax_respone->addAssign('trash1', 'innerHTML', $sScript);
       // danh sach nhan vien di lam 
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 1";      
       $rs = $objDbSelect->Execute($sql);
       $sScript = '';  
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : '';  
                $sScript .= '';
                $username = $rs->fields['username']; 
                if ($url_image != '')
                {
                  $sScript .='<div  id="'.$id_image.'" class="gallery" style="width:48px;height:36px;display: inline-block;padding-right: 10px;margin-top: 0px">';
                  $sScript .= ' <img src="'.$url_image.'" id="'.$id_image.'" style="width:48px;height:36px"/>';  
                  $sScript .= '<h5 class="ui-widget-header" style="width:47x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
                  $sScript .= '</div>';         
                     
                }
           }                                           
           while($rs->MoveNext());    
       }
        $i = 2;  
        $js .=' var $trash'.$i.' = $( "#trash'.$i.'" );    
                      $( "div", $trash'.$i.' ).draggable({
                        revert: "invalid", 
                        containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
                        helper: "clone",
                        cursor: "auto"
                        });
                      $trash'.$i.'.droppable({
                       activeClass: "ui-state-highlight",
                       drop: function( event, ui ) {
                        var a = $.trim($($(this).find("img").get(0)).attr("id")); 
                        var b = $.trim($($(ui.draggable).get(0)).attr("id"));
                        deleteImage( ui.draggable,$trash'.$i.' ); 
                        xajax_ajxUpdateData(b,1);
                      }
                });';
       $xjax_respone->addAssign('trash2', 'innerHTML', $sScript); 
      $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 2";      
       $rs = $objDbSelect->Execute($sql);
       $sScript = '';  
       if($rs->RecordCount() > 0)
       {
           $i = 1;
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : '';
                $username = $rs->fields['username'];   
                $sScript .= '';
                if ($url_image != '')
                {
                  $sScript .='<div  id="'.$id_image.'" class="gallery" style="width:48px;height:36px;display: inline-block;padding-right: 10px;margin-top: 0px">';                              
                  $sScript .= ' <img src="'.$url_image.'" id="'.$id_image.'" style="width:48px;height:36px"/> ';  
                  $sScript .= '<h5 class="ui-widget-header" style="width:47x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
                  $sScript .= '</div>';
                }
           }                                           
           while($rs->MoveNext());    
       }
        $i = 3;  
        $js .=' var $trash'.$i.' = $( "#trash'.$i.'" );    
                      $( "div", $trash'.$i.' ).draggable({
                        revert: "invalid", 
                        containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
                        helper: "clone",
                        cursor: "auto"
                        });
                      $trash'.$i.'.droppable({
                       activeClass: "ui-state-highlight",
                       drop: function( event, ui ) {
                        var a = $.trim($($(this).find("img").get(0)).attr("id")); 
                        var b = $.trim($($(ui.draggable).get(0)).attr("id"));
                        deleteImage( ui.draggable,$trash'.$i.' ); 
                        xajax_ajxUpdateData(b,2);
                      }
                });';  
    $xjax_respone->addAssign('trash3', 'innerHTML', $sScript); 
    $xjax_respone->addScript('$(function() {
            var $gallery = $( "#gallery" );
            $( "div", $gallery ).draggable({
            revert: "invalid", 
            containment: $( "#demo-frame" ).length ? "#demo-frame" : "document",
            helper: "clone",
            cursor: "crosshair"
            });
            $gallery.droppable({
            activeClass: "custom-state-active",
            drop: function( event, ui ) {
                var a = $.trim($($(ui.draggable).get(0)).attr("id"));
                recycleImage( ui.draggable ,$gallery);
                xajax_ajxUpdateData(a,-1); 
            }
        });
        '.$js.'
        var recycle_icon = "";
        function deleteImage( $item ,$trash) {
            $item.fadeOut(function() {
                $item.find( "a.ui-icon-trash" ).remove();
                $item.append( recycle_icon ).appendTo( $trash ).fadeIn(function() {
                    $item
                        .animate({ width: "48px",height: "36px" })
                        .find( "img" )
                            .animate({ width:"48px",height: "36px"});
                });
            });
        }
        var trash_icon = "";
        function recycleImage( $item ,$gallery) {
            $item.fadeOut(function() {
                $item
                    .find( "a.ui-icon-refresh" )
                        .remove()
                    .end()
                    .css({
                         height : "72px",
                         width : "96px"      
                    })
                    .append( trash_icon )
                    .find( "img" )
                        .css( {
                         height : "72px",
                         width : "96px"   
                        } )
                    .end()
                    .appendTo( $gallery )
                    .fadeIn();
            });
            }
    });'); 
   return $xjax_respone -> getXML(); 
  }                 
   catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
}
// trường hợp nhấn stop
function ajxUpdateData($id_employee,$status)
{
  global $objDbSelect;
   try
   {
   $xjax_respone = new xajaxResponse();
   $SQL = ""; 
   if ($status == -1)
   {
     $SQL = "DELETE FROM tbl_current_employee WHERE id_employee = $id_employee";   
   }
   else
   {
       $sSQL2 = "";
       $sql = "SELECT id FROM tbl_current_employee  WHERE id_employee = $id_employee";  
//       print_r($sql);
//       die();    
       $rs = $objDbSelect->Execute($sql);  
       $time = date('Y-m-d H:i');
       $now = date('Y-m-d');
       $timestop = "";
        if ($status == 1)
        {
         $timestop = "0000-00-00 00:00:00"; 
        }
        else
        {
         $timestop = $time;   
        }
       if($rs->RecordCount() > 0)
        {
            
            $SQL = "UPDATE tbl_current_employee SET `status` = $status WHERE id = ".$rs->fields['id']; 
            $sSQL2 = "UPDATE tbl_history_employee SET `status` = $status,endtime = '$timestop' WHERE id_employee = $id_employee AND DATE_FORMAT(createdate,'%Y-%m-%d') = '$now'";   
        }
       else
        {
           $SQL = "INSERT INTO tbl_current_employee(id_employee,`status`,createdate) VALUES($id_employee,$status,'$now')";
           $sSQL2 = "INSERT INTO tbl_history_employee(id_employee,`status`,createdate,endtime) VALUES($id_employee,$status,'$time','$timestop')";        
        }
     $objDbSelect->Execute($sSQL2);    
   }
   $objDbSelect->Execute($SQL); 
   }
   catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
  return $xjax_respone -> getXML();             
}   

?>
